<template>
    <div class="box">
        <h1 v-if="info.cityInfo">
            {{ info.cityInfo.parent }}
            {{ info.cityInfo.city }}
            {{ info.cityInfo.updateTime }}
            <a-input-search class="input" v-model:value="city" placeholder="城市名" enter-button @search="getData" />
        </h1>
        <BaseEChart class="echarts" :chartOption="amountRankOption" />
    </div>
    <div class="box">
        <h1 v-if="info.cityInfo">
            {{ info.cityInfo.parent }}
            {{ info.cityInfo.city }}
            {{ info.cityInfo.updateTime }}
        </h1>
        <BaseEChart class="echarts" :chartOption="option" />
    </div>
</template>

<script setup lang="ts">
import BaseEChart from '@/components/BaseEcharts/index.vue'
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import { weatherInfo15 } from '@/api/index'
const city = ref("嘉兴")
const amountRankOption: any = ref({
    title: {
        text: '未来15日 天气预测',
        textStyle: {
            fontSize: 20
        }
    },
    tooltip: {
        trigger: 'axis',
        textStyle: {
            fontSize: 20
        }
    },
    legend: {
        textStyle: {
            fontSize: 18
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: [],
        axisLabel: {
            fontSize: 18
        }
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '{value} °C',
            fontSize: 18
        }
    },
    series: [
        {
            name: '最高温度',
            type: 'line',
            lineStyle: {
                color: '#faa755',
                width: 2
            },
            data: [],
            label: {
                show: true,
                position: 'top',
                fontSize: 16
            },
            areaStyle: {
                opacity: 0.8,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: 'rgba(255, 191, 0,0.1)'
                    },
                    {
                        offset: 1,
                        color: '#faa755'
                    }
                ])
            },
        },
        {
            name: '最低温度',
            type: 'line',
            data: [],
            smooth: true,
            label: {
                show: true,
                position: 'top',
                fontSize: 16
            },
            lineStyle: {
                color: 'rgb(77, 119, 255)',
                width: 2
            },
            areaStyle: {
                opacity: 0.8,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: 'rgba(0, 221, 255,0)'
                    },
                    {
                        offset: 1,
                        color: 'rgb(77, 119, 255)'
                    }
                ])
            },
        }
    ]
})
const option: any = ref({
    title: {
        text: '气温对比'
    },
    tooltip: {
        trigger: 'axis',
        textStyle: {
            fontSize: 20
        }
    },
    legend: {
        data: ['最高温', '最低温'],
        textStyle: {
            fontSize: 18
        }
    },
    calculable: true,
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: [],
        axisLabel: {
            fontSize: 18
        }
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '{value} °C',
            fontSize: 18
        }
    },
    series: [
        {
            name: '最高温',
            type: 'bar',
            data: [
            ],
            label: {
                show: true,
                position: 'top',
                fontSize: 16
            },
        },
        {
            name: '最低温',
            type: 'bar',
            data: [],
            label: {
                show: true,
                position: 'top',
                fontSize: 16
            },
        }
    ]
})


const info: any = ref({})
const getData = async () => {
    try {
        const res = await weatherInfo15({ cityname: city.value })
        if (!res.data) return
        info.value = res
        const data = res.data.forecast
        const regex = /\d+/g
        const dateArrs: any = []
        const lowArrs: any = []
        const highArrs: any = []
        data.forEach((el: any) => {
            dateArrs.push(el.ymd)
            lowArrs.push(el.low.match(regex)[0])
            highArrs.push(el.high.match(regex)[0])
        })
        amountRankOption.value.xAxis.data = dateArrs
        amountRankOption.value.series[0].data = highArrs
        amountRankOption.value.series[1].data = lowArrs
        option.value.xAxis.data = dateArrs
        option.value.series[0].data = highArrs
        option.value.series[1].data = lowArrs
    } catch (error) {
        console.log(error);
    }
}

onMounted(() => {
    getData()
})
</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 410px;

    h1 {
        margin: 0;
        padding: 0;
        text-align: center;
        height: 60px;
        line-height: 60px;
        font-size: 30px;
        background-color: #113;
        color: white;
        position: relative;

        .input {
            width: 110px;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
        }
    }

    .echarts {
        width: 100%;
        height: 350px;
    }
}
</style>